<?php



namespace app\home\controller;
use think\Session;

class User extends \think\Controller
{
    public function login(){
//        $user = Model("User");
//        $user = new User;
        $user_id = Session::get('id');
        if ($user_id) {
            $this->redirect('user/showlist');
        }
        if($_POST){
            $captcha = $_POST['captcha'];
            $referurl = $_POST['referurl'];
            if(!captcha_check($captcha)){
                $this->error( "验证码错误!");   
        };
//            var_dump($_POST);exit;
            $name = $_POST['name'];
            $password = $_POST['password'];
//            $data = $user->where("'name'='$name'")->find();
           $data = db('user')->where('name',$name)->find();
//            var_dump($data);exit;
            if($data){
                if($data['password'] == $password){
//                  $this->redirect('register'); 
                    $id = $data['id'];
                    Session::set('id',$id);
                    Session::set('name',$name);
                    $this->success('登录成功',$referurl);
                } else {
                $this->error( "密码错误!");   
                }
            }else{
                 $this->error( "账号不存在!");   
            }
        }else{
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Home/Index/index';
        if($referurl=="http://tp5shop.io:8080/home/user/register.html"){
            $referurl='Home/Index/index';
        }
            
        $this->assign('referurl',$referurl);
        return $this->fetch('login');
            
        }
    }
    public function checkCode(){
        $data = input('post.');
        $msg = $this->validate($data,[
            'verify|验证码'=>'require|captcha'
        ]);
        if($msg === true){
            echo "true";
        }else{
            echo "false";
        }
        
        
        
    }
    public function doregister(){
        $name = input('param.name');
        $data = db('user')->where('name',$name)->find();
       if($data){
           echo "false";
       }else{
           echo "true";
       }
   } 
   public function register(){
       if($_POST){
//       dump(input("param.name"));exit;
//          $captcha = $_POST['verify'];
//            if(!captcha_check($captcha)){
//                $this->error( "验证码错误!");   
//        };
           $name = $_POST['name'];
           $password = $_POST['password'];
//           $repassword = $_POST['repassword'];
//           $data = db('user')->where('name',$name)->find();
//           if($data){
//               $this->error('用户名已存在');
//           }else{
//             if($password !== $repassword){
//               $this->error('两次密码不一样');
//             }
             $arr=['name'=>$name,'password'=>$password];
            $data = db('user')->insert($arr);
            if($data){
            $this->success('注册成功','showList');
           }
            $this->error('注册失败');
       }
       
        return $this->fetch('register');
    } 
    
    public function logout(){
//        Session::delete('id');
//        Session::delete('name');
        Session::clear();
        $this->success('退出成功','index/index');
    }
    
    public function showList(){
       return $this->fetch('showList'); 
    }
    
   public function order(){
        return $this->fetch('order');
    } 
    
   public function address(){
       
       
        return $this->fetch('address');
    } 
    
}
